package com.crl.ssc.dic.dao;

import com.cars.util.page.PageUtil;
import com.cars.util.string.StringUtil;
import com.crl.dao.BaseDao;
import com.crl.ssc.dic.model.DicCity;
import com.crl.ssc.dic.vo.DicCitySearchVO;
import org.springframework.stereotype.Repository;

import java.util.List;


/**
 * @description: 字典管理-城市字典Dao
 * @author: 孔垂云
 * @version: 1.0
 * @date: 2020-08-03 18:38:21
 */
@Repository
public class DicCityDao extends BaseDao<DicCity, DicCitySearchVO> {
    /**
     * 新增
     *
     * @param dicCity
     * @return
     */
    public int add(DicCity dicCity) {
        String sql = "INSERT INTO td_dic_city(province_code,city_code,city_name) " +
                "VALUES(:provinceCode,:cityCode,:cityName)";
        return insert(sql, dicCity);
    }

    /**
     * 修改
     *
     * @param dicCity
     * @return
     */
    public int update(DicCity dicCity) {
        String sql = "UPDATE td_dic_city SET province_code=:provinceCode,city_name=:cityName " +
                    "WHERE city_code=:cityCode";
        return update(sql, dicCity);
    }

    /**
     * 删除
     *
     * @param cityCode
     * @return
     */
    public int delete(String cityCode) {
        String sql = "DELETE FROM td_dic_city WHERE city_code=?";
        return delete(sql, cityCode);
    }
    /**
     * 列表
     *
     * @param dicCitySearchVO
     * @return
     */
    public List<DicCity> list(DicCitySearchVO dicCitySearchVO) {
        String sql = "SELECT province_code,city_code,city_name FROM td_dic_city WHERE 1=1 ";
        sql += createSearchSQL(dicCitySearchVO);
        sql += " ORDER BY city_code ASC";
        sql = PageUtil.createMysqlPageSql(sql, dicCitySearchVO.getPage());
        return list(sql, dicCitySearchVO);
    }
    /**
     * 查询数据条数
     *
     * @param dicCitySearchVO
     * @return
     */
    public int count(DicCitySearchVO dicCitySearchVO) {
        String sql = "SELECT COUNT(city_code) FROM td_dic_city WHERE 1=1 ";
        sql += createSearchSQL(dicCitySearchVO);
        return count(sql, dicCitySearchVO);
    }
    /**
       * 拼接查询条件
       *
       * @param dicCitySearchVO
       * @return
    */
    private String createSearchSQL(DicCitySearchVO dicCitySearchVO) {
        String sql = "";
        if (StringUtil.isNotNullOrEmpty(dicCitySearchVO.getProvinceCode())) {
			sql += " AND province_code =:provinceCode ";
		}if (StringUtil.isNotNullOrEmpty(dicCitySearchVO.getCityCode())) {
			sql += " AND city_code =:cityCode ";
		}if (StringUtil.isNotNullOrEmpty(dicCitySearchVO.getCityName())) {
			sql += " AND city_name =:cityName ";
		}
        return sql;
    }

    /**
     * 获取单个
     *
     * @param cityCode
     * @return
     */
    public DicCity get(String cityCode) {
        String sql = "SELECT province_code,city_code,city_name FROM td_dic_city WHERE city_code=?";
        return get(sql, cityCode);
    }
}
